home *** CD-ROM | disk | FTP | other *** search
- Introduction. Logo is a computer programming language which was
- designed to be both simple to use and extremely powerful. It was designed
- by a group of computer scientists at MIT and at Bolt, Beranek, and Newman.
- Its structure is based largely on the LISP language, which is widely used
- in Artificial Intelligence research, but the notation has been changed to
- make Logo far easier for a beginner than LISP.
-
- The power of Logo comes primarily from the idea of the procedure. A
- procedure is simply something the computer "knows" how to do; some pro-
- cedures are built into Logo itself (these are called primitive procedures),
- while others are defined by the programmer in terms of these simple pro-
- cedures. Defined procedures can be used as part of the definition of other
- procedures, so that a complicated program can be built in "layers" of com-
- plexity. This layered structure is analogous to the description of a com-
- plex machine in terms of building blocks: an automobile is made of a
- chassis, a drive train, an electrical system, and so on. The drive train
- contains an engine, a transmission, a clutch, an axle, and so on. The
- transmission contains a housing, gears, and levers. A lever may include
- connecting joints at the ends and at a pivot point. Eventually the
- description of the automobile reaches the level of bolts and washers; these
- correspond to the primitive procedures in Logo.
-
- Starting_Logo. Start the Logo interpreter from WorkBench or from the
- CLI. When Logo is running it will print a question mark (?) at the be-
- ginning of a line to indicate that it is ready for you to type in a Logo
- instruction. The instruction may print something on the terminal, or draw
- a line on a graphics display screen. Then another question mark is typed
- and you may give another instruction. (If Logo prints a "greater than" sign
- (>) instead of a question mark, it is in list input mode, which
- will be described later. Type your system quit character (control-G) to
- return to normal mode.)
-
- Syntax. Unlike most computer languages, Logo has an almost entirely
- uniform syntax. That is, all of the different commands Logo understands
- are represented using the same notation: the name of a procedure is fol-
- lowed by its inputs, which may be constants (like numbers) or else may be
- the results of using other procedures. Here is a simple example:
-
- print "hello
-
- In this Logo instruction, the primitive procedure print is used with a con-
- stant input, the word hello. The quotation mark indicates that hello is
- being used to represent the word itself; without the quotation mark it
- would have been interpreted as the name of a procedure, just as print is
- the name of a procedure. In Logo, the print procedure requires at least
- one input, which is the thing to print. The input can be a word, as in
- this example, or a list, which will be explained later. (A number is a
- special case of a word, and a sentence is a special case of a list.) Here
- is another example:
-
- print first "hello
-
- Here, the primitive procedure first is being used, also. It has one input,
- a word, and has an output which is the first letter of the word. The out-
- put from first is used as the input to print, so what is printed is the
- letter h rather than the word hello as in the earlier example.
-
- Don't confuse the output from a procedure with what is printed by the
- print command. In Logo, the word "output" is not used to refer to what is
- printed by a program, just as the word "input" does not mean something you
- type into the program. Instead, these words refer to objects (words or
- lists) which are given to a procedure (inputs) or produced by a procedure
- (outputs). A particular procedure will have a fixed number (possibly zero)
- of required inputs, and may have optional inputs. It may or may not pro-
- duce an output. A procedure with an output (like first) is called an oper-
- ation; one without an output (like print) is called a command.
-
- Some operations only have two possible outputs: the word TRUE and the
- word FALSE. Such a procedure is called a predicate. Predicates are used
- to allow a program to carry out some instruction only if a particular con-
- dition is met. By convention, predicates generally have names ending with
- the letter "p".
-
- Multiple_inputs_to_operations. Several Logo primitive procedures are
- operations with two inputs. The arithmetic operations, like sum, are exam-
- ples of this. A special extension to Logo syntax allows such an operation
- to have more than two inputs by enclosing the operation and its inputs in
- parentheses:
-
- ( sum 2 5 13 8.5 )
-
- Multi-instruction_lines. It is possible to put more than one instruc-
- tion on the same line when you are typing to Logo. For example,
-
- print "hello print "goodbye
-
- Later in this manual, the phrase "instruction line" will mean one or more
- instructions on a line.
-
- Multi-line_instructions. It is possible to continue an instruction on
- a second line. At the end of the first line, close with a tilde (~) to let
- Logo know you want to continue:
-
- repeat 5 [ print ~
- "Greetings! ]
-
- Here, Greetings! will be printed five times.
-
- Comments. It is possible to include comments in an instruction line
- which are meant for human readers of your program (including yourself, next
- week), and which are not Logo instructions. To do this, begin the comment
- with an semi-colon ";". Everything after the semi-colon on the line will
- be ignored by Logo. For example:
-
- print [ Hi, there. ] ; A friendly greeting.
-
- However, the semi-colon does not begin a comment if it is part of a word or
- list (see below). You must type a space before the semi-colon (if it is
- not at the beginning of a line) and after it, as in the example above, to
- make sure it will be interpreted as the beginning of a comment.
-
- Words. Every computer language deals with particular kinds of ob-
- jects. Most languages, like FORTRAN or BASIC or Pascal, are best at deal-
- ing with numbers. Logo is a list processing language, which is at its best
- with more complicated data structures. The two main categories of object
- are the word and the list.
-
- A word is a string of characters. A character is a letter, digit,
- space, or punctuation mark. A word can be any length, including zero. The
- way to indicate a word as part of a Logo program is to use the quotation
- mark (") before the word. The word begins with the character after the
- quotation mark and continues until a space, or the end of the line occurs.
- A quotation mark immediately followed by a space or RETURN indicates the
- empty word, which is a word of length zero.
-
- Please notice that, unlike most programming languages, Logo does not
- use quotation marks in pairs to delimit strings of characters. The follow-
- ing instruction is incorrect:
-
- print "aardvark"
-
- It will print `aardvark"'; not `aardvark'.
-
- In order to include a space in a word, you must precede it with a
- backslash (\). Do not confuse backslash with the regular slash (/) char-
- acter. For example, this instruction:
-
- print "Hello\ there
-
- will print `Hello there' as its result. To include a backslash in a word,
- precede it with another backslash (\\).
-
- Numbers. A number is a special case of a word, in which the charac-
- ters are all digits. (That definition isn't quite complete, and will be
- expanded in the next paragraph.) A number need not be preceded with a quo-
- tation mark. (This rule is possible because normally Logo interprets words
- without quotation marks as the names of procedures, but there are no pro-
- cedures whose names are all digits.)
-
- Actually, numbers may be written in scientific notation. That is,
- they can include signs, decimal points, and a power of 10 by which the
- number is multiplied. This exponent is indicated by the letter e followed
- by the integer power of 10. The following numbers have the same value:
-
- 1000
- "1000
- 1000.00
- 1e3
- 10.0E+2
- "+1.0e3
- 10000E-1
-
- A number which contains only digits (no decimal point or exponent) is
- called an integer.
-
- Since a number is a word, the usual character-manipulating procedures
- may be applied to it. For example,
-
- print first 1024
-
- prints the digit 1 which is the first character of the number. In addi-
- tion, there are arithmetic procedures which apply specifically to numbers:
-
- print sum 3 2
-
- prints the number 5. These procedures will be listed later.
-
- Lists. A word can be thought of as a list of characters; for example,
- the word hello is a list of five letters. In Logo it is possible to mani-
- pulate not only lists of characters but also lists of words, lists of lists
- of words, and so on. This is a very powerful capability which allows very
- complicated data structures to be manipulated easily. To indicate a list
- in a program, you put square brackets ([ and ]) around the list, and
- separate the list elements with spaces. For example:
-
- print [ This is a list of seven words. ]
-
- A list all of whose elements are words is called a sentence. Here is an
- example of a list which is not a sentence:
-
- print [ [ This is a list ] [ of two sentences. ] ]
-
- Within a bracketed list, square brackets delimit sub-lists (lists
- which are elements of the main list). The quotation mark, parentheses, and
- braces are not considered special within a bracketed list, unlike the rules
- for quoted words. A list may extend over more than one line; that is, if
- you have typed an open square bracket ([) and have not yet typed the match-
- ing close square bracket, the Logo instruction is not ended by typing the
- RETURN key.
-
- Variables. A variable is an entity which has a name, which is a word,
- and a thing (also called a value), which can be any Logo object. Variables
- are used to "remember" a computed object for repeated or delayed use in a
- program. In Logo, the most common way that a variable acquires a value is
- that it is associated with an input to a user-written procedure. In the
- following example, don't worry about the details of the format of the pro-
- cedure, which will be explained later:
-
- to pff :in.sentence
- print first first :in.sentence
- end
-
- This is the definition of a command with one input. The name of the com-
- mand is pff. It has one input following the procedure name (For more
- about multiple and optional inputs, check "usermanual.doc".) The variable
- whose name is in.sentence is associated with the first (and only, in this
- case) input to pff. In the line starting with the word print, the notation
- :in.sentence means "the value of the variable whose name is in.sentence".
- (To refer to the name itself, quote it as you would any word.) If this
- procedure is used in a Logo instruction like this:
-
- pff [ This is the poop. ]
-
- then the variable in-sentence has the value [ This is the poop. ].
-
- It is also possible to assign a value to a variable by an explicit
- Logo instruction. There is a primitive procedure to do this:
-
- make - Command, two inputs.
- The first input is the name of a variable (that is, it must be a
- word); the second is any Logo object. The effect of the command is to
- assign the second input as the value of the variable named by the
- first input.
-
- Ifyou are accustomed to programming in a non-procedural language like
- BASIC, you should strenuously avoid the temptation to overuse make; expli-
- cit assignment is almost always the wrong thing to do in Logo. Total
- abstention is the best policy for a Logo beginner.
-
- In Logo, variables are dynamically scoped. That means that a variable
- can "belong to" a particular procedure; such a variable can be used by that
- procedure and by any procedure which is used by an instruction within the
- procedure, but is not available to the procedure which invoked the owning
- procedure. In other words, such a local variable comes into being when the
- owning procedure starts running, and disappears when that procedure is fin-
- ished. It is possible for a procedure with a local variable to use another
- procedure with a local variable of the same name. In that case, the vari-
- able belonging to the "inner" procedure is the one which is associated with
- the name as long as it exists; when the inner procedure is finished, the
- "hidden" variable belonging to the outer procedure is again available.
-
- A variable which is associated with inputs to a procedure is local to
- that procedure. Other variables are global: they are "permanent" and do
- not disappear when the procedure in which they get their values finish.
- See "userman.doc" for details concerning inputs and local variables.
-
- The virtue of local variables is that they make procedures more inde-
- pendent of one another than they would be if global variables were used.
- In other words, if you use local variables consistently, then nothing that
- happens in one procedure will change the values of variables used in
- another procedure. This makes it very much easier to find program errors.
-
- Primitive_procedures_to_define_user_procedures. Procedures can be
- typed into the Logo interpreter at the question mark prompt, or loaded in
- from a file. In either case, the make command is used to construct the
- procedure definitions.
-
- to
- This command accepts the name of a procedure to be defined, followed
- by the names of inputs. Then follows one or more lines of the Logo
- instructions to be performed when the procedure is executed. The
- procedure ends with the word "end".
-
- printout
- The input to this command is a word or a list of words that are proce-
- dure or variable names. The command prints out a `to command' def-
- inition for that name.
-
- erase
- The input to this command is a word or a list of words that are proce-
- dure or variable names. The command removes the bindings of the input
- names.
-
- Primitive_procedures_to_manipulate_words_and_lists. There are primi-
- tive procedures to print text objects on the terminal, to read them from
- the terminal, to combine them into larger objects, to split them into
- smaller objects, and to determine their size and nature:
-
- print
- The input, which may be a word or a list, is printed on the terminal,
- followed by a new line character. (That is, the terminal is posi-
- tioned at the beginning of a new line after printing the object.) If
- the object is a list, any sub-lists are delimited by square brackets,
- but the entire object is not delimited by brackets.
-
- type
- The input, which may be a word or a list, is printed on the terminal,
- without a new line character. (That is, the terminal remains posi-
- tioned at the end of the object after printing it.) Brackets are used
- as with the print command.
-
- show
- The input is printed as by the print command, except that if it is a
- list (as opposed to a word) it is enclosed in square brackets.
-
- readlist
- Logo waits for a line to be typed by the user. The contents of the
- line are made into a list, as though typed within square brackets as
- part of a Logo instruction. (The user should not actually type brack-
- ets around the line, unless s/he desires a list of one element, which
- is a list itself.) That list is the output from the operation.
-
- word
- The two inputs must be words. The output is a word which is the con-
- catenation of the two inputs. There is no space or other separation
- of the two inputs in the output.
-
- sentence
- The two inputs may be words or lists. The output is a list formed
- from the two inputs in this way: if either input is a word, that word
- becomes a member of the output list; if either input is a list, the
- members of that input become members of the output. Here are some ex-
- amples:
-
- first input second input output
- "hello "test [ hello test ]
- "goodbye [ cruel world ] [ goodbye cruel world ]
- [ a b ] [ c d ] [ a b c d ]
- [ ] "garply [ garply ]
-
- If an input is the empty list, as in the last example above, it con-
- tributes nothing to the output.
-
- list
- The output is a list of two elements, namely, the two inputs. The in-
- puts may be words or lists.
-
- fput
- The first input may be any Logo object;
- if the first input is a list, the second must also be a list.
- The output is an object which is identical to the second input
- except with the first input at the beginning.
-
- lput
- The first input may be any Logo object;
- if the first input is a list, the second must also be a list.
- The output is an object which is identical to the second input
- except with the first input at the end.
-
- first
- The input may be any non-empty Logo object. If the input is a list,
- the output is its first member. If the input is a word, the output is
- a single-letter word, namely the first letter of the input. If the
- input is empty (a word or list of length zero) an error results.
-
- last
- The input may be any non-empty Logo object. If the input is a list,
- the output is its last member. If the input is a word, the output is
- a single-letter word, namely the last letter of the input. If the in-
- put is empty (a word or list of length zero) an error results.
-
- butfirst
- The input may be any non-empty Logo object. If the input is a list,
- the output is a list equal to the input list with the first member re-
- moved. (If the input list has only one member, the output is the emp-
- ty list, a list of zero members.) If the input is a word, the output
- is a word equal to the input word with the first letter removed. (If
- the input is a single-letter word, the output is the empty word.) If
- the input is empty, an error results.
-
- butlast
- The input may be any non-empty Logo object. If the input is a list,
- the output is a list equal to the input list with the last member re-
- moved. (If the input list has only one member, the output is the emp-
- ty list, a list of zero members.) If the input is a word, the output
- is a word equal to the input word with the last letter removed. (If
- the input is a single-letter word, the output is the empty word.) If
- the input is empty, an error results.
-
- count
- The input may be any Logo object. If the input is a list, the output
- is a number indicating the number of members in the list. (Note: only
- top-level members are counted, not members of members. The count of
- the list
-
- [ [ This is ] [ a list ] ]
-
- is 2, not 4.) If the input is a word, the output is the number of
- letters (or other characters) in the word. Remember that in Logo a
- number is just a particular kind of word, so the output from count can
- be manipulated like any other Logo word.
-
- emptyp
- The input can be any Logo object. The output is the word true if the
- input is of length zero (i.e., it is the empty word or the empty
- list). The output is the word false otherwise.
-
- equalp
- The inputs can be any Logo objects. The output is the word true if
- the two inputs are identical. That is, they must be of the same type
- (both words or both lists), they must have the same count, and their
- members (if lists) or their characters (if words) must be identical.
- The output is the word false otherwise.
-
- wordp
- The input can be any Logo object. The output is the word true if the
- input is a word. The output is the word false if the input is a list.
-
- memberp
- If the second input is a word, the first input must be a word (and
- only the first letter is used), and the output is true if and only if
- the first input is contained in the second as a character. If the
- second input is a list, the first input can be any Logo object, and
- the output is true if and only if the first input is a member of the
- second input. (Note that this is member, not subset.)
-
- item
- The first input must be a positive integer less than or equal to the
- count of the second input. If the second input is a word, the output
- is a word of length one containing the selected character from the
- word. (Items are numbered from 1, not 0.) If the second input is a
- list, the output is the selected member of the list.
-
- Primitive_procedures_for_turtles_and_graphics. An important part of
- the Logo environment is a rich set of applications to which the computer
- can be directed. The most important of these is turtle geometry, a way of
- describing paths of motion in a plane which is well-suited to computer pro-
- gramming.
-
- forward
- The input is a number, the distance you would like the turtle to move.
- The turtle moves in whatever direction it is pointing when you use the
- command. Negative inputs make the turtle go backwards.
-
- back
- The input is a number, a distance to move, as in the forward command.
- The difference is that the turtle moves backward, i.e., in the direc-
- tion exactly opposite to the way it's pointing. Negative inputs make
- the turtle go forward.
-
- left
- The input is a number, the number of degrees of angle through which
- the turtle should turn counterclockwise. This command does not change
- the position of the turtle, but merely its heading (the direction in
- which it points). Negative inputs make the turtle turn right.
-
- right
- The input is a number; the turtle turns through the specified number
- of degrees clockwise. Negative inputs make the turtle turn left.
-
- pu
- This command tells the turtle to raise its pen from the paper, so that
- it does not leave a trace when it moves. The effect is that any for-
- ward or back commands after this point will not draw a line. The tur-
- tle starts with its pen down.
-
- pd
- This command tells the turtle to lower its pen, so that later commands
- will draw lines when the turtle moves.
-
- clean
- It erases everything in the turtle display window, but does not affect
- the turtle.
-
- seth
- The turtle's heading is set to the input.
-
- toward
- The inputs are the x and y coordinates of a point. The output is a
- number which is the heading to which the turtle must be set, in order
- to point towards that point from its current position. Note: this
- operation does not actually move or turn the turtle. You must use it
- as the input to seth if that is what you want.
-
- heading
- The output is the turtle's current heading.
-
- Primitive_procedures_for_arithmetic. Several procedures are available
- for arithmetic operations on numbers. In all cases, the inputs to these
- procedures must be numbers, except as otherwise indicated in the individual
- descriptions.
-
- In general, procedures are used in Logo by typing first the name of
- the procedure, then its inputs. This is true of arithmetic procedures
- also, e.g.,
-
- sum 3 2
- quotient 4 2
-
- Logo also supports infix operators, which are placed between the two
- numbers to be operated upon:
-
- 3 + 2
- 4 / 2
-
- sum, +
- The output of this procedure is the sum of the two inputs.
-
- difference, -
- The output of this procedure is the difference of the two inputs.
-
- product, *
- The output of this procedure is the product of the two inputs.
-
- quotient, /
- The output of this procedure is the quotient of the two inputs.
-
- remainder
- The output is the remainder of dividing the first input by the second.
-
- >
- The output of this procedure is the word true if the first input is
- numerically strictly greater than the second input. Otherwise the
- output is the word false.
-
- <
- The output of this procedure is the word true if the first input is
- numerically strictly less than the second input. Otherwise the output
- is the word false.
-
- equalp, =
- The two inputs to this procedure may be any Logo objects. If they are
- numbers, then the output is the word true if they are numerically
- equal, false if they are numerically unequal. If either input is not
- a number, then the output is the same as for the procedure: it is true
- if the two inputs are identical, false if not.
-
- numberp
- The input may be any Logo object. The output is the word true if the
- input is a number, false if not.
-
- =0
- The input must be a number. The output is the word true if the input
- is numerically equal to zero, false otherwise.
-
- random
- The output is a randomly selected integer between 0 and one less than
- the input.
-
- sqrt
- The input must be a nonnegative number. The output is its square root.
-
- power
- The inputs must be numbers. If the first is negative, the second must
- be an integer. The output is the first number raised to the power of
- the second input.
-
- sin
- The input must be numeric. The output is the sine of the input.
-
- cos
- The input must be numeric. The output is the cosine of the input.
-
- atan
- The input must be numeric. The output is the arctangent of the input.
-
- Primitive_procedures_for_conditional_execution. The predicates (like
- wordp) which we've mentioned above can be used to carry out some command
- only if a condition is met. The basic command for the purpose is if:
-
- if
- The first input to the if procedure must be either the word true or
- the word false. Typically, it is the output from a predicate. The
- second and third inputs are lists containing instruction lines. The
- second input is executed if the first input is true. The third input
- is executed if the first input is false:
-
- and
- The two inputs must both be either true or false. The output is true
- if both inputs are true; otherwise the output is false.
-
- or
- The two inputs must be either true or false. The output is true if at
- least one of the inputs is true; otherwise the output is false.
-
- not
- The input must be either true or false. The output is true if the in-
- put is false, and vice versa.
-
- Primitive_procedures_for_procedure_exit. A procedure written by a
- user, in Logo, can be a command or an operation. If it is an operation,
- you must, in the procedure, say what its output should be. If it is a com-
- mand, it can simply stop at the end of the procedure, or you can explicitly
- make it stop before the end.
-
- output
- This command is used in a user procedure which is meant to be an
- operation. The input to this command becomes the output from the user
- procedure. Please don't be confused by the fact that the user pro-
- cedure is an operation, while the output primitive procedure is a com-
- mand used in that procedure. Example:
-
- to nickname :person
- if equalp :person [ Peter Parker ] [ output "Spiderman ]
- if equalp :person [ Lamont Cranston ] [ output "Shadow ]
- output first :person
- end
-
- stop
- This command is used in user procedures which are meant to be com-
- mands. It stops the user procedure. (Note that it does not stop all
- running procedures. If user procedure A runs user procedure B, a stop
- command in procedure B returns to procedure A, which continues after
- the point where procedure B was invoked.)
-
- toplevel
- This command stops all running procedures. The user at the terminal
- is prompted to type another command. This can be used when a user
- procedure discovers some error condition and wants to abort the entire
- program, for example.
-
- Property_lists. It is possible to associate with any name a list of
- "properties". A property list contains property names and property values.
- For example:
-
- putprop "bh "firstname "Brian
- putprop "bh "lastname "Harvey
-
- The form of a property list is
-
- [ name1 val1 name2 val2 name3 val3 ]
-
- Although this data structure could be created using other Logo primitives,
- special property list primitives are provided because they are faster. The
- following primitives manipulate property lists.
-
- putprop
- The first input, which must be a word, is a name with which a property
- list is associated. The second input, which must be a word, is the
- name of a property. The third input can be any Logo object. It be-
- comes the value of the specified property of the specified name.
-
- getprop
- Both inputs must be words. The first is a name, and the second is a
- property name. The output is the value of the indicated property of
- the indicated object. It is not an error if there is no such proper-
- ty; the output in that case is the empty list.
-
- remprop - Command, two inputs.
- The inputs must be words, as for gprop. The specified property is re-
- moved from the specified name.
-
- Pausing. When you are debugging a complicated Logo program, it is
- very helpful to be able to stop in the middle of a procedure, so that you
- can give interactive commands to examine its inputs and other local vari-
- ables. This is different from stopping a procedure, which destroys its lo-
- cal environment. There are two ways a procedure can pause: (1) You can use
- the interrupt command (found in the Utilities file) in the procedure defin-
- ition, to make the procedure pause at a particular place you choose in ad-
- vance; or (2) you can decide to pause a procedure while it is running by
- using the interrupt menu item in the Utilities menu of the command window.
-
- Note that when you type the system "quit" character (control-G) Logo
- does not pause, but returns to toplevel. All information about the local
- state of your active procedures is lost.
-
- When you are paused, Logo accepts instructions from your terminal as
- it does at toplevel, but local variables can be examined or modified. To
- let you know that you are paused, Logo prompts with the characters "-->"
- instead of just "?" as usual.
-
- To get out of a pause, you can give the command toplevel, which stops
- all pending procedures and returns to interactive top level, or you can
- give the command 'stop', which will resume the procedure at the point
- where you paused.
-
- interrupt
- This command is meaningful only within a procedure. It causes a
- pause.
-
- Miscellaneous_primitives. The remaining primitives are one of a kind,
- or very obscure, or both.
-
- bye
- This command is used to leave Logo. It is the only way out, unless
- there is a bug somewhere.
-
- thing
- The input must be a word that is a variable or procedure name. The
- output is the value of the variable. These are equivalent:
-
- :foo
- thing "foo
-
- namep
- The input must be a word. The output is true if that word is the name
- of a variable which has a value assigned to it, false otherwise.
-
- wait
- The input must be a positive integer. Logo waits that many seconds
- before continuing.
-
- doscommand
- The input is a list containing a valid CLI command to be executed.
-
- run
- The input must be a list containing Logo instructions. The list is
- run as if you typed it directly to Logo. The run procedure can be
- used as an operation, if its input is a Logo expression which produces
- a value, instead of a complete instruction:
-
- print run [ sum 2 3 ]
-
- repeat
- The first input must be a positive number. The second is an instruc-
- tion list, as for the run command. The list is run repeatedly, the
- number of times specified by the first input:
-
- repeat 5 [ print "hello ]
-
-